package com.jinyou.chengfengshop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tuo.customview.VerificationCodeView;
import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.MainActivity;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.utils.CountDownTimerUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;

/**
 * 验证验证码
 * Created by zhuzhiqiang on 2018/9/19.
 */

public class IdentifyingCodeActivity extends AppCompatActivity{

    private Button cutdownBtn;
    private Button next;
    private String phone;
    private String telCode;
    private VerificationCodeView icv;
    private TextView back_tv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identifyingcode);
        phone = getIntent().getStringExtra("phone");
        icv = (VerificationCodeView) findViewById(R.id.icv);
        final VerificationCodeView codeView = new VerificationCodeView(getApplicationContext());
        //codeView.setEtNumber(number);
        codeView.setPwdMode(false);


        cutdownBtn = (Button) findViewById(R.id.btn_cutdown);
        cutdownBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reLogin();
            }
        });



        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(cutdownBtn, 30000, 1000);

        mCountDownTimerUtils.start();



        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                telCode = icv.getInputContent();//获取到的输入内容

                if(telCode.length() == 6){
                    identifyCode();
                }
//                Intent mIntent = new Intent(IdentifyingCodeActivity.this,MainActivity.class);
//                startActivity(mIntent);
            }
        });
        back_tv =(TextView) findViewById(R.id.back_tv);
        back_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //重新登陆
    private void reLogin(){
        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edt = sh.edit();
        edt.putString("tocken","");
        edt.putString("info","");
        edt.commit();
        Intent mIntent = new Intent(IdentifyingCodeActivity.this,LoginActivity.class);
        startActivity(mIntent);
        finish();
    }
    //验证验证码
    private void identifyCode(){
        HashMap<String,String> paramsMap = new HashMap<>();

        paramsMap.put("username",phone);
        paramsMap.put("telCode",telCode);
        paramsMap.put("deviceType","android");
        paramsMap.put("sysCustomer","o2o-chengfeng");

        OkhttpUtil.okHttpPost(AppUrl.HOME_USER_REGISTER_BYTELCODE, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(IdentifyingCodeActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {

                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");
                    if(code == 1){
                        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
                        SharedPreferences.Editor edt = sh.edit();
                        edt.putString("tocken",obj.getString("token"));
                        edt.putString("info",response);
                        edt.commit();
                        Intent mIntent = new Intent(IdentifyingCodeActivity.this,MainActivity.class);
                        startActivity(mIntent);
                        finish();

                    }else {
                        Toast.makeText(IdentifyingCodeActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }


}
